package com.fsc.fragment;

import java.util.Calendar;
import com.fsc.fragment.flag.Flag;
import com.fsc.fragment.flag.FlagDao;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddFlagActivity extends Activity{
	private static final int DATE_DIALOG_ID = 0;
	private TextView tv_title;
	private Button btn_save,btn_cancel;
	private EditText et_flag,et_time;
	private int mYear,mMonth,mDay;
	private int id;
	private Intent objIntent;

	FlagDao flagDao = new FlagDao(AddFlagActivity.this);
	Flag objFlag = new Flag();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_flag);
		
		initView();
		
		objIntent=getIntent();
		int type=objIntent.getIntExtra("type",-1);
		
		//给时间控件设置监听
		et_time.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);//显示为日期选择对话框
			}
		});
		
		final Calendar c =Calendar.getInstance();//获取当前系统日期
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		updateDisplay();//显示系统当前时间
		//给btn_cancel设置监听
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		if(type==3){
			//3.代表新增，1.代表更新
			btn_save.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String flag = et_flag.getText().toString();
					if(!flag.isEmpty()){
						objIntent = new Intent();
						objIntent.putExtra("flag", et_flag.getText().toString());
						objIntent.putExtra("time",et_time.getText().toString());
						setResult(3, objIntent);
						finish();
					}else{
						Toast.makeText(AddFlagActivity.this,"请输入便签内容", Toast.LENGTH_LONG).show();
					}
				}
			});
		}else if(type==1){//1.代表更新
			tv_title.setText("管理便签");
			btn_save.setText("编辑");
			et_time.setText("");
			
			id=objIntent.getIntExtra("id",1);
			et_flag.setText(objIntent.getStringExtra("flag"));
			et_time.setText(objIntent.getStringExtra("time"));
			
			btn_save.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					objFlag.setFlag(et_flag.getText().toString());
					objFlag.setTime(et_time.getText().toString());
					objFlag.setId(id);
					flagDao.updateFlag(objFlag);
					setResult(1, objIntent = getIntent());
					finish();
				}
			});
		}
	}
	//给控件初始化
	private void initView() {
		tv_title = (TextView) findViewById(R.id.tv_title);
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		et_flag = (EditText)findViewById(R.id.et_flag);
		et_time = (EditText) findViewById(R.id.et_time);
	}
	private void updateDisplay() {
		et_time.setText(new StringBuilder().append(mYear).append("-").append(mMonth+1)
				.append("-").append(mDay));
	}
	
	//重写onCreateDialog
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) 
		{
			case DATE_DIALOG_ID:
			  return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
		}
		return null;
	}
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};
}